<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
      layout:decorator="themes/default/layout/public">

<head>
    <link th:href="@{/static/js/summernote/summernote.css}" rel="stylesheet"/>
</head>
<div  layout:fragment="content">
    <div class="row mt-10">
        <ol class="breadcrumb">
            <li><a th:href="@{/questions}">问答</a></li>
            <li class="active">发起提问</li>
        </ol>
        <form th:object="${question}" id="questionForm" method="POST" role="form" th:action="@{/question/create}">
            <input type="hidden" id="tagNames" name="tagNames" value="" />
            <input type="hidden" name="toUserId" th:value="${toUser?.id}" />
            <div th:class="${#fields.hasErrors('title')}?'form-group has-error':'form-group'">
                <label for="title"><th:block th:if="${toUser!=null}"> 正在向 <a th:href="|/space/${toUser?.id}|" th:text="${toUser?.username}"></a> 提问 </th:block> <th:block th:if="${toUser==null}">请将您的问题告诉我们 </th:block> :</label>
                <input id="title" type="text" name="title"   class="form-control input-lg" placeholder="请在这里概述您的问题" th:value="${title}" />
                <p class="help-block" th:if="${#fields.hasErrors('title')}" th:text="${#fields.errors('title')}"></p>
            </div>

            <div id="titleSuggest" class="panel hide widget-suggest panel-default">
                <div class="panel-body">
                    <p>
                        <strong>这些问题可能有你需要的答案</strong>
                        <button type="button" class="widget-suggest-close btn btn-default btn-xs ml-10">关闭提示</button>
                    </p>
                    <ul id="suggest-list" class="list-unstyled widget-suggest-list"></ul>
                </div>
            </div>
            <div th:class="${#fields.hasErrors('description')}?'form-group has-error':'form-group'">
                <label for="question_editor">问题描述(选填)</label>
                <div id="question_editor"><th:block th:text="${description}"></th:block></div>
                <p class="help-block" th:if="${#fields.hasErrors('description')}" th:text="${#fields.errors('description')}"></p>
            </div>
            <div class="row">
                <div class="col-md-5">
                    <div th:class="${#fields.hasErrors('category')}?'form-group has-error':'form-group'">
                        <select name="categoryId" id="categoryId" class="form-control">
                            <option value="0">请选择分类</option>
                            <option th:each="category:${categorys}" th:value="${category.id}" th:text="${category.name}"></option>
                        </select>
                        <p class="help-block" th:if="${#fields.hasErrors('category')}" th:text="${#fields.errors('category')}"></p>
                    </div>
                </div>
                <div class="col-md-7">
                    <div class="form-group">
                        <select id="select_tags" name="select_tags" class="form-control" multiple="multiple" >
                        </select>
                    </div>
                </div>
            </div>

            <div class="row mt-20">
                <div class="col-xs-12 col-md-11">
                    <ul class="list-inline">

                        <li class="pull-right" th:if="${GlobalSetting.irrigation.codeCreateQuestion}">
                            <div th:class="form-group" th:classappend="${validateCodeError}?'has-error'">
                                <input type="text" class="form-control" name="captcha" required="" placeholder="验证码" />
                                <span th:if="${validateCodeError}" th:text="${validateCodeError}" class="help-block"></span>
                                <div class="mt-10"><a href="javascript:void(0);" id="reloadCaptcha"><img src="validateCodeError"></a></div>
                            </div>
                        </li>

                        <li>
                            <select name="price">
                                <option selected="selected" value="0">0</option><option value="3">3</option><option value="5">5</option><option value="10">10</option><option value="15">15</option><option value="30">30</option><option value="50">50</option><option value="80">80</option><option value="100">100</option>
                            </select>&nbsp;金币
                        </li>
                        <li><input type="checkbox" name="hide" value="1" />&nbsp;匿名</li>
                    </ul>
                </div>
                <div class="col-xs-12 col-md-1">
                    <input type="hidden" id="question_editor_content"  name="description" th:value="${description}"  />
                    <button type="submit" class="btn btn-primary pull-right" >发布问题</button>
                </div>
            </div>
        </form>

    </div>
    <link th:href="@{/static/js/select2/css/select2.min.css}" rel="stylesheet"/>
    <link th:href="@{/static/js/select2/css/select2-bootstrap.min.css}" rel="stylesheet"/>
    <script th:src="@{/static/js/summernote/summernote.min.js}"></script>
    <script th:src="@{/static/js/summernote/lang/summernote-zh-CN.min.js}"></script>
    <script th:src="@{/static/js/select2/js/select2.min.js}"></script>
    <script th:src="@{/static/js/select2/js/i18n/zh-CN.js}"></script>
    <script type="text/javascript" th:inline="javascript">
        var suggest_timer = null;
        $(function() {
            $('#question_editor').summernote({
                    lang: 'zh-CN',
                    height: 180,
                    placeholder:'您可以在这里继续补充问题细节',
                    toolbar: [ ['common', ['style','bold','ol','link','picture','clear','fullscreen']] ],
                    callbacks: {
                    onChange:function (contents, $editable) {
                        var code = $(this).summernote("code");
                        $("#question_editor_content").val(code);
                    },
                    onImageUpload: function(files) {
                        upload_editor_image(files[0],'question_editor');
                    }
                }
            });

            //$('#register_editor').summernote('code',[[${question.description}]]);

            /*suggest处理*/
            $("#title").keydown(function(){

                if(suggest_timer){
                    clearTimeout(suggest_timer);
                }
                suggest_timer = setTimeout(function() {
                    var title = $("#title").val();
                    if( title.length > 1 ){
                        $.ajax({
                            url: '/question/suggest',
                            type:'post',
                            data:'word='+title,
                            cache: false,
                            success: function(html){
                                if(html == ''){
                                    $("#suggest-list").html('<li>没有找到相似问题！</li>');
                                    return;
                                }
                                $(".widget-suggest").removeClass("hide");
                                $("#suggest-list").html(html);
                            }
                        });
                    }
                }, 500);
            });

            $(".widget-suggest-close").click(function(){
                $(".widget-suggest").addClass("hide");
            });


        });
    </script>
</div>